package com.coldworks.coldjoke.ui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coldworks.coldjoke.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class JokeTabBar extends RelativeLayout {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private Button tabAll;
	private Button tabHot;
	private Button tabShit;
	private boolean[] checked = new boolean[] { true, false, false };
	private RadioGroup tabIndicator;
	private OnTabChangeListener listener;
	private Context context;

	public JokeTabBar(Context context) {
		super(context);
		setUpViews(context);
	}

	public JokeTabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setUpViews(context);
	}

	public JokeTabBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setUpViews(context);
	}

	public void setUpViews(final Context context) {
		LinearLayout view = (LinearLayout) LayoutInflater.from(context)
				.inflate(R.layout.navigation_joke_tab_bar, this, false);
		this.context = context;
		tabIndicator = (RadioGroup) view.findViewById(R.id.joke_tab_indicator);
		tabAll = (Button) view.findViewById(R.id.tab_all);
		tabHot = (Button) view.findViewById(R.id.tab_hot);
		tabShit = (Button) view.findViewById(R.id.tab_shit);
		tabAll.setTextColor(context.getResources().getColor(R.color.text_cold));
		tabHot.setTextColor(context.getResources().getColor(
				R.color.text_color_2));
		tabShit.setTextColor(context.getResources().getColor(
				R.color.text_color_2));
		tabIndicator = (RadioGroup) view.findViewById(R.id.joke_tab_indicator);
		tabAll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checked[0]) {
					listener.onTabClickedAgain(0);
					return;
				}
				setTabSelected(0);
				listener.onTabChanged(0);

			}
		});
		tabHot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checked[1]) {
					listener.onTabClickedAgain(1);
					return;
				}
				setTabSelected(1);
				listener.onTabChanged(1);
			}
		});
		tabShit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checked[2]) {
					listener.onTabClickedAgain(2);
					return;
				}
				setTabSelected(2);
				listener.onTabChanged(2);

			}
		});
		addView(view);
	}

	public void setOnTabChangeListener(OnTabChangeListener listener) {
		this.listener = listener;
	}

	public interface OnTabChangeListener {
		void onTabChanged(int tab);

		void onTabClickedAgain(int tab);
	}

	public void setTabSelected(int tab) {
		switch (tab) {
		case 0:
			checked[0] = true;
			checked[1] = false;
			checked[2] = false;
			tabAll.setTextColor(context.getResources().getColor(
					R.color.text_cold));
			tabHot.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabShit.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabIndicator.check(R.id.tab_all_indicator);
			break;

		case 1:
			checked[0] = false;
			checked[1] = true;
			checked[2] = false;
			tabAll.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabHot.setTextColor(context.getResources().getColor(
					R.color.text_cold));
			tabShit.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabIndicator.check(R.id.tab_hot_indicator);
			break;
		case 2:
			checked[0] = false;
			checked[1] = false;
			checked[2] = true;
			tabAll.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabHot.setTextColor(context.getResources().getColor(
					R.color.text_color_2));
			tabShit.setTextColor(context.getResources().getColor(
					R.color.text_cold));
			tabIndicator.check(R.id.tab_shit_indicator);
			break;
		default:
			break;
		}
	}

}